<?php

namespace app\admin\controller;

use app\admin\model\Member;
use app\admin\model\TransactionHistory;
use app\admin\model\WithdrawOrder;
use app\admin\model\RechargeOrder;
use think\Db;

class Money extends AdminBase
{
    public function WithdrawList()
    {
        $where = '1=1';
        $keywords = input('keywords');
        $status = intval(input('status'));
        if ($status) {
            $where .= " and status = " . $status;
        }
        if ($keywords) {
            $where .= " and uname like" . "'%" . $keywords . "%'";
        }
        $data = WithdrawOrder::getInstance()->getList($where, 10);
        $this->assign("data", $data['data']);
        $this->assign("domain", $this->domain);
        $this->assign("page", $data['page']);
        $this->assign("keywords", $keywords);
        $this->assign("status", $status);
        return $this->fetch();
    }

    public function RechargeList()
    {
        $where = '1=1';
        $keywords = input('keywords');
        if ($keywords) {
            $where .= " and uname like" . "'%" . $keywords . "%'";
        }
        $data = RechargeOrder::getInstance()->getList($where, 10);
        $this->assign("data", $data['data']);
        $this->assign("domain", $this->domain);
        $this->assign("page", $data['page']);
        $this->assign("keywords", $keywords);
        return $this->fetch();
    }

    /**
     * @Title: checkWithdraw
     * @Describe 更改提现状态
     * @return array
     * @author lijunwei
     * @Date 2021/11/10 14:31
     */
    public function checkWithdraw()
    {
        // 启动事务
        Db::startTrans();
        try {
            $orderId = input('orderId');
            $status = input('status');
            $param = array(
                'status' => $status,
            );
            WithdrawOrder::getInstance()->editInfo((int)$orderId, $param);
            if ((int)$status == 3) {
                // 拒绝逻辑处理（拒绝时要把手续费返回用户）
                $where = ['id' => (int)$orderId];
                $info = WithdrawOrder::getInstance()->getOne($where);
                // 用户信息
                $member = Member::getInstance()->getOne(['id' => $info['uid']]);
                // 用户金额退回
                $from_data = [
                    'id' => $info['uid'],
                    'updated_time' => time(),
                    'usdt_money' => bcadd($member['usdt_money'], $info['source_money'], 10)
                ];
                Member::getInstance()->editMember($info['uid'], $from_data);
            }
            // 提交事务
            Db::commit();
            return [
                'code' => 200,
                'msg' => '更新成功'
            ];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            errorLog('checkWithdraw', $e);
            return [
                'code' => 0,
                'msg' => '更新失败'
            ];
        }
    }

    /**
     * @Title: checkRecharge
     * @Describe 更改充值状态
     * @return array
     * @author lijunwei
     * @Date 2021/11/10 14:31
     */
    public function checkRecharge()
    {
        // 启动事务
        Db::startTrans();
        try {
            $orderId = input('orderId');
            $status = input('status');
            $param = array(
                'status' => (int)$status,
            );
            RechargeOrder::getInstance()->updateOrderData(['id' => (int)$orderId], $param);
            if ((int)$status == 1) {
                $where = ['id' => (int)$orderId];
                $info = RechargeOrder::getInstance()->getOne($where);
                // 用户信息
                $member = Member::getInstance()->getOne(['id' => $info['uid']]);
                $data_list = [
                    'uid' => $info['uid'],
                    'to_uid' => $info['uid'],
                    'amount' => $info['num'],
                    'type' => 1,   // 变动原因 1 充值
                    'change_type' => 1,   // 金额变动 1 增加
                    'account_type' => 1,   // 账户类型 1 usdt账户
                    'status' => 1,
                    'recharge_id' => $info['id'],
                    'order_number' => $info['order_number'],
                    'add_time' => time(),
                ];
                // 记录到日志中
                TransactionHistory::getInstance($info['uid'])->addOne($data_list);
                // 用户金额增加
                $from_data = [
                    'id' => $info['uid'],
                    'updated_time' => time(),
                    'usdt_money' => bcadd($member['usdt_money'], $info['num'], 10)
                ];
                Member::getInstance()->editMember($info['uid'], $from_data);
            }
            // 提交事务
            Db::commit();
            return [
                'code' => 200,
                'msg' => '更新成功'
            ];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            errorLog('checkWithdraw', $e);
            return [
                'code' => 0,
                'msg' => '更新失败'
            ];
        }
    }
}
